package com.q.tqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.model.RankInfo;
import com.q.tqb.view.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TweeterAdapter extends RecyclerView.Adapter<TweeterAdapter.ViewHolder> {
    private ArrayList<RankInfo> mGrabRedDatas;
    private Context mContext;


    public TweeterAdapter(Context context, ArrayList<RankInfo> mGrabRedDatas) {

        this.mGrabRedDatas = mGrabRedDatas;
        this.mContext = context;


    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tweeter, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {


        holder.mTvRank.setText(String.format("%d", position + 1));
        RankInfo grabRedData = mGrabRedDatas.get(position);
        holder.mTvName.setText(grabRedData.getNickname());

        if (grabRedData.getIs_users_agent() == 1) {
            holder.mIvTweeter.setVisibility(View.VISIBLE);
        } else {
            holder.mIvTweeter.setVisibility(View.GONE);
        }

        holder.mTvMoney.setText("抢红包:" + grabRedData.getGet_money() + "元");
        Glide
                .with(mContext)
                .load(grabRedData.getHeader())
                .error(R.drawable.ic_head)
                .placeholder(R.drawable.ic_head)
                .dontAnimate()
                .into(holder.mCivHead);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // mOnItemClickListener.onClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mGrabRedDatas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_head)
        CircleImageView mCivHead;

        @BindView(R.id.tv_name)
        TextView mTvName;

        @BindView(R.id.tv_rank)
        TextView mTvRank;

        @BindView(R.id.tv_money)
        TextView mTvMoney;

        @BindView(R.id.iv_tweeter)
        ImageView mIvTweeter;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


}